package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.CourseReqDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.QueryCourseDetailDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value="课程信息编辑接口",tags="课程信息编辑接口")
@RestController
@RequestMapping("/course")
public class CourseBaseInfoController {
    @Autowired
    CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("分页查询课程基本信息")
    //要有这个权限才能访问下面的方法
//    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    @PostMapping("/list")
    public PageResult<CourseBase> queryByPage(PageParams pageParams,
            @RequestBody(required = false) QueryCourseParamsDto dto){
        Long companyId = 1232141425L;
        return courseBaseInfoService.queryCourseBaseList(companyId,pageParams,dto);
    }

    @ApiOperation("新增课程")
    @PostMapping
    public CourseBaseInfoDto addCourse(
            @RequestBody @Validated(value={ValidationGroups.Insert.class})
            CourseReqDto dto){
        Long companyId = 1232141425L;
        return courseBaseInfoService.addCourse(companyId,dto);
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("/{id}")
    public CourseBaseInfoDto getCourseBaseInfoDtoById(@PathVariable Long id){
        //用户通过springsecurity认证通过后，信息会存入SecurityContextHolder上下文中，
        //SecurityContext与当前线程进行绑定，方便获取用户信息
//        Object principal = SecurityContextHolder.getContext()
//                .getAuthentication().getPrincipal();
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        return courseBaseInfoService.getCourseBaseInfoDtoById(id);
    }

    @ApiOperation("更新课程")
    @PutMapping
    public CourseBaseInfoDto updateCourse(
            @RequestBody @Validated(value={ValidationGroups.Insert.class})
            CourseReqDto dto){
        Long companyId = 1232141425L;
        return courseBaseInfoService.updateCourse(companyId,dto);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    public void deleteCourse(@PathVariable Long id){
        Long companyId = 1232141425L;
        courseBaseInfoService.deleteCourse(companyId,id);
    }

    @ApiOperation("根据条件查询课程基本信息")
    @PostMapping("/detail")
    public CourseBase getCourseBase(@RequestBody QueryCourseDetailDto dto){
        return courseBaseInfoService.getCourseBase(dto);
    }


}
